package presentation.data;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;

import middleware.exceptions.DatabaseException;
import presentation.gui.DefaultData;
import usecasecontrol.BrowseAndSelectController;
import business.SessionCache;
import business.exceptions.BackendException;
import business.exceptions.BusinessException;
import business.exceptions.UIException;
import business.externalinterfaces.CartItem;
import business.externalinterfaces.CustomerConstants;
import business.externalinterfaces.Product;
import business.externalinterfaces.ShoppingCart;
import business.externalinterfaces.ShoppingCartSubsystem;
import business.util.ProductUtil;
import business.util.ShoppingCartUtil;

public class BrowseSelectData {
	SessionCache ctx = SessionCache.getInstance();
	// changing data values selected by user
	String currentSelectedCatalog;
	String currentSelectedProd;

	public String getCurrentSelectedCatalog() {
		return currentSelectedCatalog;
	}

	public String getCurrentSelectedProd() {
		return currentSelectedProd;
	}
	
	public boolean isLoggedIn() {
		return (Boolean)ctx.get(CustomerConstants.LOGGED_IN);
	}
	
	public ShoppingCart getLiveShoppingCart() {
		return useCaseControl.getLiveShoppingCart();
	}

	public ShoppingCartSubsystem getShoppingCartSystem() {
		return useCaseControl.getShoppingCartSystem();
	}
	private BrowseAndSelectController useCaseControl = new BrowseAndSelectController();

	public List<String[]> getCatalogNames() throws BackendException {
		List<String> names = useCaseControl.getCatalogNames();
		List<String[]> namesAsStringArrays = new ArrayList<String[]>();
		for (String n : names) {
			String[] next = { n };
			namesAsStringArrays.add(next);
		}
		return namesAsStringArrays;
	}
	
	public String getCurrentSelectedCatalog(JTable catalogsTable) throws UIException {
		int selectedRow = catalogsTable.getSelectedRow();
		if (selectedRow >= 0) {
			currentSelectedCatalog = (String) catalogsTable.getValueAt(
					selectedRow, 0);
		} else {
			throw new UIException(MessagesUtil.SELECT_ROW_MSG);
		}
		return currentSelectedCatalog;
	}


	// for select product listener
	public String[] readProductDetailsData(JTable table, boolean useDefaultData)
			throws UIException, BackendException {
		int selectedRow = table.getSelectedRow();
		if (selectedRow < 0)
			throw new UIException("Please select a row");
		else {
			currentSelectedProd = (String) table.getValueAt(selectedRow, 0);
			if (useDefaultData) {
				DefaultData productData = DefaultData.getInstance();
				return productData.getProductDetailsData(currentSelectedProd);
			} else {
				//implement
				return new String[] {"1", "1", "1", "1"};
			}
		}
	}

	public int readSelectedProductForEdit(JTable table) throws UIException,
	BackendException {

		int selectedRow = table.getSelectedRow();

		if (selectedRow >= 0) {

			String type = (String) table.getValueAt(selectedRow, 0);
			System.out.println("edit -- " + type);

		} else {
			throw new UIException(MessagesUtil.SELECT_ROW_MSG);
		}
		return selectedRow;

	}

	public String readCartItemProdName(int pos) {
		String name = useCaseControl.readCartItemProdName(pos);
		return name;
	}

	public String readCartItemUnitPrice(String prodName)
			throws BackendException {
		String price = useCaseControl.readCartItemUnitPrice(prodName);
		return price;
	}

	// run rules on quantity and return the total price
	public void runRulesOnQuantity(String prodName, String quantityReq)
			throws DatabaseException, BusinessException {

		useCaseControl.runRulesOnQuantity(prodName, quantityReq);
	}

	public String getCartItemTotalPrice(String price, String quantityReq) {
		String totalprice = useCaseControl.getCartItemTotalPrice(price,
				quantityReq);
		return totalprice;
	}

	
	public void addCartItem(String name, String quantityReq, String totalprice,
			Integer positionOfEdit) throws BackendException {
		useCaseControl.addCartItem(name, quantityReq, totalprice,
				positionOfEdit);
	}

	public String getLiveCartTotalCost() {

		String total = useCaseControl.getLiveCartTotalCost();
		return total;
	}

	public boolean deleteItemFromCart(JTable table) throws UIException {
		int selectedRow = table.getSelectedRow();
		boolean isItemDeleted = false;
		if (selectedRow >= 0) {
			String item = (String) table.getValueAt(selectedRow, 0);

			isItemDeleted = useCaseControl
					.removeItemFromCart(item, selectedRow);

			// LOG.info("To be deleted: " + item + " at position " +
			// selectedRow);
		} else {
			String msg = "Please select an item to delete.";
			throw new UIException(msg);
		}
		return isItemDeleted;

	}
	
	

	public List<String[]> getDisplayableCartItems() {

		List<CartItem> itemList = useCaseControl.getLiveCartItems();
		System.out.println(itemList.size());

		return ShoppingCartUtil.makeDisplayableList(itemList);
	}

	// ///// make this class a singleton
	private static final BrowseSelectData instance = new BrowseSelectData();

	public static BrowseSelectData getInstance() {
		return instance;
	}

	private BrowseSelectData() {
	}
	
	
}
